//
//  YSClubCtrl.m
//  Yueshi
//
//  Created by yueshi_tech on 15/7/15.
//  Copyright (c) 2015年 悦食中国. All rights reserved.
//  俱乐部

#import "YSClubCtrl.h"
#import "YSClubDataTool.h"
#import "YSHomeCardImageView.h"
#import "YSCardList.h"
#import "YSAdvert.h"
#import "YSHeaderResult.h"
#import "YSCardImageResult.h"
#import "YSCardProdResult.h"
#import "YSCardBannerResult.h"
#import "YSCardGridResult.h"
#import "YSHomeBannerView.h"
#import "YSHomeProdListView.h"
#import "YSHomeCardGridView.h"
#import "CartView.h"


@interface YSClubCtrl ()<UIScrollViewDelegate>
@property (strong,nonatomic) UIScrollView *scrollView;
@property (assign,nonatomic) CGFloat currentY;
@property (strong,nonatomic) NSMutableArray *itemArray;
@property (strong, nonatomic) CartView *cartView;

@end

@implementation YSClubCtrl

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"兴趣小组"];
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SCREENW, SCREENH)];
    [_scrollView setAlwaysBounceVertical:YES];
    [_scrollView setDelegate:self];
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefresh)];
    header.lastUpdatedTimeLabel.hidden = YES;
    _scrollView.header = header;
    _scrollView.userInteractionEnabled = YES;
    [self.view addSubview:_scrollView];
    [_scrollView.header beginRefreshing];
    
    CartView *cartView = [[CartView alloc]initWithImage:@"btn_cart"];
    [cartView addTarget:self action:@selector(tapTitleRightItem:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem =  [[UIBarButtonItem alloc]initWithCustomView:cartView];
    _cartView = cartView;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    if(_cartView){
        NSInteger num =  [DefaultsHelper integerForKey:kCartIndex];
        [_cartView setNum:num];
    }
}

-(void)tapTitleRightItem:(UIBarButtonItem *)btnItem{
    [Utils showCartCtrl:self];
}

-(void)headerRefresh{
    [self requestData];
}

-(void)requestData{
    [YSClubDataTool getClubInfo:^(YSHomeInfoRes *result) {
        [_scrollView.header endRefreshing];
        if(result.isSucc){
            [self dealData:result];
        }else{
            ShowEnterTips(result.data);
        }
    } failure:^(NSError *error) {
        [_scrollView.header endRefreshing];
        ShowTips(kRequestFailure);
    }];
}

-(void)dealData:(YSHomeInfoRes *)result{
    
    NSArray *subviews = self.scrollView.subviews;
    for (UIView *subview in subviews) {
        if([subview isKindOfClass:[YSHomeCardImageView class]]
           ||
           [subview isKindOfClass:[YSHomeCardGridView class]]
           ||
           [subview isKindOfClass:[YSHomeBannerView class]]
           ||
           [subview isKindOfClass:[YSHomeProdListView class]]){
            [subview removeFromSuperview];
        }
    }
    self.currentY = 0;
    
    if(!_itemArray){
        _itemArray = [NSMutableArray arrayWithArray:result.cardList];
    }else{
        [_itemArray removeAllObjects];
        [_itemArray addObjectsFromArray:result.cardList];
    }
    for (YSCardList *card in _itemArray) {
        
        NSString *cardIcon =  card.cardIcon;
        NSString *cardDesc = card.cardIntrod;
        NSString *cardName = card.cardName;
        
        YSHeaderResult *headResult = [[YSHeaderResult alloc]init];
        [headResult setCardDesc:cardDesc];
        [headResult setCardIcon:cardIcon];
        [headResult setCardName:cardName];
        
        if(card.cardTpl == CARD_BIG_IMAG_STYLE){
            YSHomeCardImageView *cardImageView  = [[YSHomeCardImageView alloc]initWithSourceType:ClubSourceType];
            [cardImageView setY:self.currentY];
            [self.scrollView addSubview:cardImageView];
            
            YSCardImageResult *result = [[YSCardImageResult alloc]init];
            [result setHeadResult:headResult];
            [result setAdvert:card.advert];
            
            [cardImageView setInfo:result];
            NSLog(@"cardImageView maxY :%f",CGRectGetMaxY(cardImageView.frame));
            self.currentY = CGRectGetMaxY(cardImageView.frame);
        }
        
        if(card.cardTpl == CARD_GRID_STYLE){
            YSCardGridResult *result = [[YSCardGridResult alloc]init];
            [result setHeadResult:headResult];
            [result setAdvert:card.advert];
            
            YSHomeCardGridView *gridView = [[YSHomeCardGridView alloc]initWithSourceType:ClubSourceType];
            [gridView setY:self.currentY];
            [self.scrollView addSubview:gridView];
            [gridView setInfo:result];
            NSLog(@"gridView maxY :%f",CGRectGetMaxY(gridView.frame));
            self.currentY = CGRectGetMaxY(gridView.frame);
        }
        
        if(card.cardTpl == CARD_BANNER_STYLE){
            YSCardBannerResult *result = [[YSCardBannerResult alloc]init];
            [result setAdvert:card.advert];
            YSHomeBannerView *bannerView = [[YSHomeBannerView alloc]initWithAdvert:result sourceType:ClubSourceType];
            [bannerView setFrame:CGRectMake(0, self.currentY, SCREENW, 250)];
            [self.scrollView addSubview:bannerView];
            NSLog(@"bannerView maxY :%f",CGRectGetMaxY(bannerView.frame));
            self.currentY = CGRectGetMaxY(bannerView.frame);
        }
        
        if(card.cardTpl == CARD_DETAIL_STYLE){
            YSCardProdResult *result = [[YSCardProdResult alloc]init];
            [result setHeadResult:headResult];
            [result setProdlist:card.productList];
            
            YSHomeProdListView *prodView = [[YSHomeProdListView alloc]initWithSourceType:ClubSourceType];
            [prodView setInfo:result];
            [prodView setY:self.currentY];
            [_scrollView addSubview:prodView];
            
            NSLog(@"prodView maxY :%f",CGRectGetMaxY(prodView.frame));
            self.currentY = CGRectGetMaxY(prodView.frame);
        }
    }
    NSLog(@"current maxY = %f  current ScreenH = %f",self.currentY,SCREENH);
    [_scrollView setContentSize:CGSizeMake(SCREENW, self.currentY+BottomTabarH+NavWithStatusBarH)];
    NSLog(@"contentSize = %f",self.scrollView.contentSize.height);
}
@end
